﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.Domains.OrganizationalUnits;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MathNet.Numerics.Differentiation;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Utilities.Encoders;
using SheepCommon.API.Vendors;
using System.Diagnostics.Metrics;

namespace SheepCommon.API.SheepPurchases
{
    public class SheepPurchaseSreachHander : IRequestHandler<SheepPurchaseSreachCommand, ResultPage<SheepPurchaseDTO>>
    {
        /// <summary>
        /// 采购
        /// </summary>
        private readonly IBaseRepository<SheepPurchase> _sheepPurchase;
        /// <summary>
        /// 栋舍
        /// </summary>
        private readonly IBaseRepository<Cottage> _cottageRepository;
        /// <summary>
        /// 栏位
        /// </summary>
        private readonly IBaseRepository<Field> _fieldRepository;
        /// <summary>
        /// 供应商
        /// </summary>
        private readonly IBaseRepository<Vendor> _vendorRepository;
        //构造函数
        public SheepPurchaseSreachHander(IBaseRepository<SheepPurchase> sheepPurchase, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository, IBaseRepository<Vendor> vendorRepository)
        {
            _sheepPurchase = sheepPurchase;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
            _vendorRepository = vendorRepository;
        }

        /// <summary>
        /// 个体采购显示查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<SheepPurchaseDTO>> Handle(SheepPurchaseSreachCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<SheepPurchaseDTO>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var sheeppurchase = await (from s in await _sheepPurchase.GetAll()
                                           join c in await _cottageRepository.GetAll() on s.CottageId equals c.CottageId
                                           join f in await _fieldRepository.GetAll() on s.Field equals f.FieldId
                                           join v in await _vendorRepository.GetAll() on s.VendorId equals v.VendorId
                                           select new SheepPurchaseDTO
                                           {
                                               PurchaseId = s.PurchaseId,
                                               CottageId = s.CottageId,
                                               CottageName = c.CottageName,
                                               FieldName = f.FieldName,
                                               VendorName = v.VendorName,
                                               PurchaseName = s.PurchaseName,
                                               PurchaseTime = s.PurchaseTime,
                                               SheepType = s.SheepType,
                                               PurchaseCategory = s.PurchaseCategory,
                                               AccountingUnit = s.AccountingUnit,
                                               Abstract = s.Abstract,
                                               PrchaseNumber = s.PrchaseNumber,
                                               RecordAuricle = s.RecordAuricle,
                                               Sex = s.Sex,
                                               GoodName = s.GoodName,
                                               Package = s.Package,
                                               Measurement = s.Measurement,
                                               Storage = s.Storage,
                                               PurchasePrice = s.PurchasePrice,
                                               PurchaseNumPrice = s.PurchaseNumPrice,
                                               PurchaseDiscount = s.PurchaseDiscount,
                                               PurchaseAdjust = s.PurchaseAdjust,
                                               PurchaseSettle = s.PurchaseSettle,
                                               PurchaseWeight = s.PurchaseWeight,
                                               WareHouse = s.WareHouse,
                                               CreatedOwnerId = s.CreatedOwnerId,
                                               CreatedTime = s.CreatedTime,
                                               UpdatedOwnerId = s.UpdatedOwnerId,
                                               UpdatedTime = s.UpdatedTime,
                                           }).ToListAsync();
                if (request.SheepType != 0)//羊只类型
                {
                    sheeppurchase = sheeppurchase.Where(s => s.SheepType == request.SheepType).ToList();
                }
                if (request.PurchaseTime != null)//采购日期
                {
                    sheeppurchase = sheeppurchase.Where(s => s.PurchaseTime == request.PurchaseTime).ToList();
                }
                if (!string.IsNullOrEmpty(request.WareHouse))//仓库
                {
                    sheeppurchase = sheeppurchase.Where(s => s.WareHouse == request.WareHouse).ToList();
                }
                if (request.CottageId != 0)//栋舍
                {
                    sheeppurchase = sheeppurchase.Where(s => s.CottageId == request.CottageId).ToList();
                }
                if (!string.IsNullOrEmpty(request.RecordAuricle))//耳号
                {
                    sheeppurchase = sheeppurchase.Where(s => s.RecordAuricle.Contains(request.RecordAuricle)).ToList();
                }
                if (!string.IsNullOrEmpty(request.PurchaseName))//采购员
                {
                    sheeppurchase = sheeppurchase.Where(s => s.PurchaseName.Contains(request.PurchaseName)).ToList();
                }
                if (request.VendorId != 0)//供应商
                {
                    sheeppurchase = sheeppurchase.Where(s => s.VendorId == request.VendorId).ToList();
                }
                if (!string.IsNullOrEmpty(request.PrchaseNumber))//单据号
                {
                    sheeppurchase = sheeppurchase.Where(s => s.PrchaseNumber == request.PrchaseNumber).ToList();
                }
                result.TotalCount = sheeppurchase.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                sheeppurchase = sheeppurchase.OrderByDescending(c => c.PurchaseId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = sheeppurchase;
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }
    }
}
